<?php
/**
 * @package  nuo
 * @author   Nam Nguyen <nam.ngvan@gmail.com>
 * @version  1.0
 * @since    Apr 17, 2013
 */
class Nuo_Response
{


	/**
	 * HTTP Header constants
	 */
	/**
	 * Cache header control
	 */
	const CACHE_CONTROL = 'Cache-Control';

	/**
	 * Content Diposition
	 */
	const DISPOSITION = 'Content-Disposition';

	/**
	 * Content Length
	 */
	const LENGTH = 'Content-Length';

	/**
	 * Connection
	 */
	const CONNECTION = 'Connection';

	/**
	 * Prag ma
	 */
	const PRAGMA = 'Pragma';

	/**
	 * Content Type
	 */
	const CONTENT_TYPE = 'Content-Type';

	/**
	 * Expire time
	 */
	const EXPIRES = 'Expires';

	/**
	 * Last modified
	 */
	const LAST_MODIFIED = 'Last-Modified';

	/**
	 * Location
	 */
	const LOCATION = 'Location';

	/**
	 * Headers to send with response
	 *
	 * @var array
	 */
	protected $_headers = array();

	/**
	 * Content data of return document
	 * @access   protected
	 * @var      string/array
	 */
	protected $_content = '';

	/**
	 * Set content data
	 * @access  public
	 * @param   string/array   $xhtml
	 * @return  Nuo_Response
	 */
	function setContent($content)
	{
		$this -> _content = $content;
		return $this;
	}

	/**
	 * Get content data
	 * @access  public
	 * @return  string/array
	 */
	function getContent()
	{
		return $this -> _content;
	}

	/**
	 * @return Nuo_Response
	 */
	public function clearContent()
	{
		$this -> _content = '';
		return $this;
	}

	/**
	 * Adds headers to response.
	 *
	 * @param string $name
	 * @param string $value
	 */
	public function setHeader($name, $value)
	{
		$this -> headers[trim($name)] = trim($value);
	}

	/**
	 * Clears all headers.
	 */
	public function clearHeaders()
	{
		$this -> headers = array();
	}

	/**
	 * Sends all added headers.
	 */
	public function sendHeaders()
	{
		if (!headers_sent())
		{
			foreach ($this->_headers as $headerName => $headerValue)
			{
				if (substr(mb_strtolower($headerName), 0, 4) === 'http')
				{
					header($headerName . ' ' . $headerValue);
				}
				else
				if (mb_strtolower($headerName) === 'status')
				{
					header(ucfirst(mb_strtolower($headerName)) . ': ' . $headerValue, null, (int)$headerValue);
				}
				else
				{
					header($headerName . ':' . $headerValue);
				}
			}
		}
	}

	/**
	 * Execute main requestion
	 * concept about template will be remove from here.
	 * move to response object
	 * @static
	 * @access public
	 * @param  Request   $request  Optional
	 * @return string
	 */
	public function output()
	{

		$request = Nuo::getRequest();

		$request -> execute();

		$this -> sendHeaders();

		$format = $request -> getParam('format');

		if ('gbox' == $format)
		{
			$breadcrum = Nuo::getVar('breadcrum');

			$title = 'Untitled Title';
			if (isset($breadcrum['title']))
			{
				$title = $breadcrum['title'];
			}
			return json_encode(array(
				'html' => Nuo::getView() -> render('core/layout/master/fbox', array(
					'request' => $request,
					'title' => $title
				)),
				'error' => 0,
				'message' => ''
			));
		}
		else
		if ('jscript' == $format)
		{
			return '<script type="text/javascript">' . $this -> _content . '</script>';
		}
		else
		if ($format == 'json' or $request->isAsync())
		{
			if (!is_string($this -> _content))
			{
				$this -> _content = json_encode($this -> _content);
			}

			if ('json' == $format)
			{
				@header('Content-Type: text/html;charset=UTF-8');
				return json_encode(array(
					'html' => $this -> _content,
					'error' => 0,
					'message' => ''
				));
			}
			else
			{
				return $this->_content;
			}
			return $this->_content;
		}
		else
		if ('xml' == $format)
		{
			return $this -> _content;
		}
		else
		{
			//@header('Content-Type: text/xhtml;;charset=UTF-8');
			return Nuo::getDocument() -> render();
		}
	}

}
